//
//  MyfansViewController.m
//  Findif
//
//  Created by Mac on 2018/11/8.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "MyfansViewController.h"
#import "MyFansTableViewCell.h"
#import "MyAttentionModel.h"

@interface MyfansViewController ()<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource,MyFansTableViewCellDelegate>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, weak)UITextField * searchTextfiled;
@property (nonatomic, strong)NSArray<MyAttentionModel *> * attentionArray;
@property (nonatomic, weak)UILabel * showLabel;

@end

@implementation MyfansViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = ColorWithRGB(0xF7F8F9);
    
    [self loadDataWithSearch:@""];
    [self setupNav];
    [self setupUI];
}


- (void)loadDataWithSearch:(NSString *)search{
    
    self.attentionArray = nil;
    NSString * user_id = DEF_GET_OBJECT(@"id");
    [[ZRNetworkTool sharedTool] getFansUserUser_id:user_id WithSearch:search ResultWithBlock:^(BOOL success, id responseData) {
        
//        NSLog(@"res===%@",responseData);
        if (success) {
            
            self.attentionArray = [NSArray yy_modelArrayWithClass:[MyAttentionModel class] json:responseData[@"data"]];
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseData[@"message"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }];
}

- (void)setAttentionArray:(NSArray<MyAttentionModel *> *)attentionArray{
    
    _attentionArray = attentionArray;
    
    [self.tableView reloadData];
}

- (void)setupNav{
    
    self.title = @"我的粉丝";
    
//    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back(1)"] style:UIBarButtonItemStylePlain target:nil action:nil];
//    
//    self.navigationItem.backBarButtonItem = backItem;
}


- (void)setupUI{
    
    UITextField * searchTextfiled = [[UITextField alloc] initWithFrame:CGRectMake(45 * SCREENH6, kNavHeight + kStatusBarHeight + 10 * SCREENH6, 286 * SCREENH6, 30 * SCREENH6)];
    
    searchTextfiled.borderStyle = UITextBorderStyleRoundedRect;
    searchTextfiled.backgroundColor = ZRWhiteColor;
    //设置光标颜色
    searchTextfiled.tintColor = ZRBlackColor;
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = ColorWithRGB(0xbebebe);
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    searchTextfiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"快速搜索你的粉丝" attributes:attrs];
    searchTextfiled.textColor = [UIColor blackColor];
    searchTextfiled.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22 * SCREENH6, 0)];
    //设置显示模式为永远显示(默认不显示)
    searchTextfiled.leftViewMode = UITextFieldViewModeAlways;
    searchTextfiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    UIView * rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 35 * SCREENH6, 20 * SCREENH6)];
    UIImageView *image=[[UIImageView alloc] initWithFrame:CGRectMake(5, 0, 16 * SCREENH6, 17.5 * SCREENH6)];//WithImage:[UIImage imageNamed:@"手机号"]];
    image.image = [UIImage imageNamed:@"hg"];
    [rightView addSubview:image];
    searchTextfiled.leftView = rightView;
    searchTextfiled.leftViewMode = UITextFieldViewModeAlways;
    searchTextfiled.returnKeyType = UIReturnKeySearch;
    searchTextfiled.delegate = self;
    
    self.searchTextfiled = searchTextfiled;
    
    [self.view addSubview:self.searchTextfiled];
    
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(15 * SCREENH6, kNavHeight + kStatusBarHeight + 50 * SCREENH6, 345 * SCREENH6, kScreenH - kNavHeight - kStatusBarHeight - 50 * SCREENH6) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ZRWhiteColor;
    tableView.rowHeight = 60 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[MyFansTableViewCell class] forCellReuseIdentifier:@"MyFansTableViewCell"];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self updateData];
    }];
    
    [self.view addSubview:self.tableView];
    
    UILabel * showLabel = [UILabel new];
    
    showLabel.textColor = ZRBlackColor;
    showLabel.text = @"您还没有粉丝哦~";
    showLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    showLabel.textAlignment = NSTextAlignmentCenter;
    //    showLabel.hidden = YES;
    
    self.showLabel = showLabel;
    
    [self.view addSubview:self.showLabel];
}

- (void)viewDidLayoutSubviews{
    
    [self.showLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.centerY.mas_equalTo(self.view).offset(50 * SCREENH6);
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    if (self.attentionArray.count == 0) {
        
        self.showLabel.hidden = NO;
    }else{
        
        self.showLabel.hidden = YES;
    }
    return self.attentionArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyFansTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyFansTableViewCell" forIndexPath:indexPath];
    
    cell.delegate = self;
    cell.model = self.attentionArray[indexPath.row];
    
    return cell;
}

//实现UITextField代理方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];//取消第一响应者
    
    [self loadDataWithSearch:textField.text];
    
    return YES;
}

- (void)clickFansItemID:(NSString *)ID WithType:(NSString *)type{
    
    if ([type isEqualToString:@"cancel"]) {
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"确定不再关注此人？"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction * action) {
                                                                  
                                                                  [self attentionPersonWith:ID];
                                                              }];
        UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
                                                             handler:^(UIAlertAction * action) {
                                                                 
                                                             }];
        
        [alert addAction:defaultAction];
        [alert addAction:cancelAction];
        [self presentViewController:alert animated:YES completion:nil];
    }else{
        
        [[ZRNetworkTool sharedTool] attentionUseroWithUser_id:ID ResultWithBlock:^(BOOL success, id responseData) {
            
            if (success) {
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:responseData[@"message"]
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          
                                                                          [self loadDataWithSearch:@""];
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
            }else{
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:responseData[@"message"]
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
            }
        }];
    }
    
}

- (void)attentionPersonWith:(NSString *)ID{
    
    [[ZRNetworkTool sharedTool] cancelAttentionUserUser_id:ID ResultWithBlock:^(BOOL success, id responseData) {
        
        if (success) {
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseData[@"message"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                      [self loadDataWithSearch:@""];
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseData[@"message"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }];
}

#pragma mark - TableView 占位图
- (NSString *)xy_noDataViewMessage {
    
    return @"";
}

- (void)updateData{
    
    NSString * user_id = DEF_GET_OBJECT(@"id");
    [[ZRNetworkTool sharedTool] getFansUserUser_id:user_id WithSearch:@"" ResultWithBlock:^(BOOL success, id responseData) {
        
        //        NSLog(@"res===%@",responseData);
        if (success) {
            
            self.attentionArray = [NSArray yy_modelArrayWithClass:[MyAttentionModel class] json:responseData[@"data"]];
        }
    }];
    
    [self.tableView.mj_header endRefreshing];
}

@end
